<template>
  <div class="content">
    <div class="login">
      <div class="login-div">
        <div class="login-wecome">
          <p class="title">Welcome Back!</p>
          <p class="dsc">
            To keep connected with us please login with your personal info
          </p>
        </div>
        <div class="login-msg">
          <div class="lognText">
            <p>Lo<span>gin</span></p>
          </div>
          <div class="loginInput">
            <div class="login-icon">
              <img src="@/assets/loginImgs/accountImg.png" alt="">
            </div>
            <input
              v-model="loginForm.account"
              type="text"
              placeholder="account"
            >
          </div>
          <div class="loginInput">
            <div class="login-icon">
              <img src="@/assets/loginImgs/psdImg.png" alt="">
            </div>
            <input
              v-if="passwordType !== 'password'"
              v-model="loginForm.password"
              type="text"
              placeholder="password"
            >
            <input
              v-if="passwordType === 'password'"
              v-model="loginForm.password"
              type="password"
              placeholder="password"
            >
            <div
              v-if="passwordType === 'password'"
              class="login-icon psd-hide"
              @click="showPwd"
            >
              <img src="@/assets/loginImgs/eyeImg.png" alt="">
            </div>
            <div
              v-if="passwordType !== 'password'"
              class="login-icon psd-hide"
              @click="showPwd"
            >
              <img src="@/assets/loginImgs/openEyeImg.png" alt="">
            </div>
          </div>
          <div class="btn1">
            <div class="btnS" @click="handleLogin">sign up</div>
          </div>
          <div class="note">
            <p>
              Don't have an account?
              <strong><router-link to="/home/register">
                register
              </router-link></strong>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { toLogin } from '@/api/login';
export default {
  data() {
    return {
      loginForm: {
        account: '',
        password: ''
      },
      copyPsd: '',
      passwordType: 'password',
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  mounted() {
    this.$nextTick(function() {
      if (
        document.documentElement.clientHeight >
        document.getElementsByClassName('content')[0].clientHeight
      ) {
        document.getElementsByClassName('content')[0].style.minHeight =
          document.documentElement.clientHeight -
          document.getElementsByClassName('header')[0].clientHeight -
          document.getElementsByClassName('footer')[0].clientHeight +
          'px';
      } else {
        document.getElementsByClassName('content')[0].style.minHeight =
          950 -
          document.getElementsByClassName('header')[0].clientHeight -
          document.getElementsByClassName('footer')[0].clientHeight +
          'px';
      }
    });
    toLogin();
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
    },
    handleLogin() {
      this.loading = true;
      if (!this.loginForm.account) {
        this.$message.warning('account name cannot be empty');
        return;
      } else {
        if (this.loginForm.account.length > 50) {
          this.$message.warning('The maximum length of the user name is 50 characters');
          return;
        }
        if (this.loginForm.account.length < 5) {
          this.$message.warning('The user name should be at least 5 characters long');
          return;
        }
      }
      if (!this.loginForm.password) {
        this.$message.warning('password cannot be empty');
        return;
      } else {
        if (this.loginForm.password.length < 8) {
          this.$message.warning(
            'Please Enter password minimum Enter eight length'
          );
          return;
        }
        if (this.loginForm.password.length > 20) {
          this.$message.warning(
            'The maximum length of the user name is 20 characters'
          );
          return;
        }
      }
      this.$store
        .dispatch('user/login', this.loginForm)
        .then(() => {
          this.$router.push({ path: this.redirect || '/' });
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.content {
  width: 100%;
  height: 100%;
  background: url("../../assets/loginImgs/loginBg.png") no-repeat;
  background-size: 100% 100%;
  margin-top: 70px;
}
.login {
  text-align: center;
  height: 890px;
  overflow: hidden;
  &-div {
    min-width: 1200px;
    height: 508px;
    margin: 0 auto;
    margin-top: 100px;
  }
  &-wecome {
    width: 330px;
    height: 100%;
    display: inline-block;
    padding-top: 195px;
    background: url("../../assets/loginImgs/loginw.png") no-repeat;
    background-size: 100% 100%;
    .title {
      font-size: 32px;
      font-weight: bold;
      line-height: 37px;
      color: #ffffff;
    }
    .dsc {
      font-size: 18px;
      font-weight: normal;
      line-height: 24px;
      padding: 10px;
      color: #ffffff;
    }
  }
  &-msg {
    width: 577px;
    height: 100%;
    display: inline-block;
    vertical-align: top;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0px 4px 10px rgba(53, 42, 42, 0.2);
    .login-icon {
      width: 25px;
      height: 25px;
      margin-right: -20px;
      vertical-align: text-bottom;
      position: absolute;
      top: 10px;
      display: inline-block;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .psd-hide {
      right: 20px;
    }
    .lognText {
      text-align: center;
      margin-top: 75px;
      margin-bottom: 50px;
      p {
        font-size: 44px;
        color: #999999;
        font-weight: bold;
        span {
          color: #02a152;
        }
      }
    }
    .loginInput {
      width: 448px;
      margin: 0 auto;
      margin-bottom: 36px;
      position: relative;
      input {
        width: 100%;
        height: 48px;
        font-size: 22px;
        padding-left: 36px;
        border: none;
        outline: none;
        box-shadow: inset 0 0 0 1000px #ffffff !important;
        border-bottom: 1px solid #cccccc;
      }
    }
    .btn1 {
      width: 100%;
      height: 54px;
      margin-bottom: 10px;
      .btnS {
        width: 218px;
        height: 54px;
        font-size: 26px;
        margin: 0 auto;
        color: #ffffff;
        cursor: pointer;
        line-height: 54px;
        background: rgba(2, 161, 82, 1);
        box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.16);
        border-radius: 4px;
      }
    }
    .note {
      p {
        color: #b8b6b6;
        font-size: 14px;
        strong {
          color: #02a152;
        }
      }
    }
  }
}
</style>
